<?php
class userController extends Controller_Base
{
    public function init()
    {
        $this->noLogInJump();
    }
    
    public function indexAction()
    {
        $this->listAction();
    }

    public function listAction()
    {
        $page = intval($this->getParam('page', 1));
        if($page <= 0) $page = 1;
        
        try {
            $cnt = Model_User::getCount();
            $users = Model_User::getList($page);
            $pageNav = Model_Navigate::getPageListBar($cnt, $page, Config_Default::getUserPageSize());
        }catch (Exception $e){
            $this->errExit($e->getTraceAsString(), $e->getMessage());
        }
        $this->setData('title', '用户列表');
        $this->setData('userlist', true);
        $this->setData('users', $users);
        $this->setData('pageNav', $pageNav);
        $this->setView('User/list.tpl');
    }
    
    public function addAction()
    {
        if(Model_User::getLevelID() != 5){
            $this->notAuthWeb();        
            return;
        }
        
        $this->setData('title', '添加用户');
        $this->setData('useradd', true);
        $this->setData('levelinfo', Config_Default::getAuthConfig());
        $this->setView('User/add.tpl');
    }
    
    public function passwordAction()
    {
        $this->setData('title', '修改密码');
        $this->setData('userpassword', true);
        $this->setView('User/password.tpl');
    }
    
    public function doAddAction()
    {
        if(Model_User::getLevelID() != 5){
            $this->notAuthAjax();        
            return;
        }
        
        $userName  = $this->getPost('username',  '');
        $userLevel = $this->getPost('userlevel',  '');
        
        try {
            Model_User::add($userName, $userLevel);
            $this->ajaxRet(0, 'success');
            return;
        }catch (Exception $e){
            $this->ajaxRet($e->getCode(), $e->getMessage());
            return;
        }
    }
    
    public function doDelAction()
    {
        if(Model_User::getLevelID() != 5){
            $this->notAuthAjax();        
            return;
        }
        
        $userID = intval($this->getPost('userid',  -1));
        
        try {
            Model_User::delete($userID);
            $this->ajaxRet(0, 'success');
            return;
        }catch (Exception $e){
            $this->ajaxRet($e->getCode(), $e->getMessage());
            return;
        }
    }
    
    public function doResetAction()
    {
        if(Model_User::getLevelID() != 5){
            $this->notAuthAjax();        
            return;
        }
        
        $userID = intval($this->getPost('userid',  -1));
        
        try {
            Model_User::reset($userID);
            $this->ajaxRet(0, 'success');
            return;
        }catch (Exception $e){
            $this->ajaxRet($e->getCode(), $e->getMessage());
            return;
        }
    }
    
    public function doPasswordAction()
    {
        $oldPassword = trim($this->getPost('oldpassword',  ""));
        $newPassword = trim($this->getPost('newpassword',  ""));
        $rePassword = trim($this->getPost('repassword',  ""));

        try {
            Model_User::password($oldPassword, $newPassword, $rePassword);
            Model_User::signOut();
            $this->ajaxRet(0, 'success');
            return;
        }catch (Exception $e){
            $this->ajaxRet($e->getCode(), $e->getMessage());
            return;
        }
    }
}